<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;

class Auth extends Controller
{
    public function __construct()
    {
        parent::__construct( );
        $this->loginauth();

//        $navData=$this->getNavData();
//        var_dump($navData);die;
//        $this->assign('navData',$navData);
    }

//    登陆验证
    private function loginauth(){
//        var_dump($url);die;
        if(!Session::get('id')){
//            var_dump($url);die;
            $url=url('login/login');
            $this->redirect($url);
        }

        if(Session::get('id')!=1){
            $controller = request()->controller();
            $action = request()->action();
            $auth = new \think\auth\Auth();
            if($action=="getnavdata"){
                return;
            }
            if(!$auth->check($controller . '-' . $action, session('id'))){
                $this->error('你没有权限访问');
            }
        }
    }

    public function getnavdata(){
        $where['uid']=Session::get('id');
        $ruleData=db('auth_rule')->where(['status'=>1])->column('id,name','id');
        if($where['uid']!=1){
        $gid=db('auth_group_access')->where($where)->column('group_id');
        $temprules=db('auth_group')->where('id','in',$gid)->column('rules');
        }else{
            return returnMsgNoDie(1,'ok',array('data'=>$ruleData));
        }
        $data=array();
        $itemarr=array();
        $field='';
        foreach($temprules as $k=>$v){
            $itemarr=explode(',',$v);
            foreach($itemarr as $kk=>$vv){
                if($vv){
                    $data[]=$ruleData[$vv];
                }
            }
        }

//        p($data);die;
        returnMsgNoDie(1,'ok',array('data'=>$data));
    }

}
